package com.qf.controller;

import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.CategorySellAmts;
import com.qf.vo.DayStatis;
import com.qf.vo.OrderAmtsVo;
import com.qf.vo.UserOrderCntVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/dashboard")
public class DtsAdminController {

    @Autowired
    DtsGoodsService goodsService;

    @Autowired
    DtsUserService userService;

    @Autowired
    DtsProductService productService;

    @Autowired
    DtsOrderService orderService;

    @Autowired
    DtsCategoryService categoryService;








    @GetMapping("/chart")
    public Object chart(){

        List<CategorySellAmts> categorySellAmtsList = categoryService.getCategorySellAmts();
        List<String> categoryNames = fetchCategoryNames(categorySellAmtsList);
        Map<String,Object> categorySell = new HashMap<>();
        categorySell.put("categoryNames", categoryNames);
        categorySell.put("categorySellData", categorySellAmtsList);

        //统计最近一个月范围内的 订单的总额和 订单笔数(包含一个月内的日期)
        List<DayStatis>  orderDayStatis = orderService.findOrderDayStatis(30);
        //提取数据中的日期字符串
        String[] dayData = fetchDayData(orderDayStatis);

        OrderAmtsVo orderAmtsVo = new OrderAmtsVo();
        orderAmtsVo.setDayData(dayData);
        //提取订单的笔数
        int[] cnt = fetchCnt(orderDayStatis, dayData);

        orderAmtsVo.setOrderCntData(cnt);
        //提取订单的总额
        BigDecimal[] orderAmtData = fetchOrderAmt(orderDayStatis, dayData);

        orderAmtsVo.setOrderAmtData(orderAmtData);


        Map<String,Object> data = new HashMap<>();
        //大类 销售额统计
        data.put("categorySell", categorySell);
        // 日期对应的订单总额  订单笔数
        data.put("orderAmts", orderAmtsVo);


        //查询一个月内 用户情况
        List<DayStatis>  userDayStatis = userService.findUserDayStatis(30);

        UserOrderCntVo userOrderCntVo = new UserOrderCntVo();
        //把user和order存储到一个数组中
        String[] userDayStr = unionDayStr(orderDayStatis, userDayStatis);
        //提取用户的日增长量
        int[] cnts = fetchCnt(userDayStatis, userDayStr);

        userOrderCntVo.setDayData(userDayStr);

        userOrderCntVo.setUserCnt(cnts);
        //提取order的日增长量
        int[] orderCnts = fetchCnt(orderDayStatis, userDayStr);

        userOrderCntVo.setOrderCnt(orderCnts);

        // 日期对应的用户数和订单数
        data.put("userOrderCnt", userOrderCntVo);

        return ResponseUtil.ok(data);
    }

    private String[] unionDayStr(List<DayStatis> orderDayStatis, List<DayStatis> userDayStatis) {
        Set<String> set  = new HashSet<>();
        //把 User和Order 结果中的日期都添加到了set集合中
        if(orderDayStatis != null && orderDayStatis.size() > 0){
            for (DayStatis dayStatis : orderDayStatis) {
                set.add(dayStatis.getDayStr());
            }
        }
        if(userDayStatis != null && userDayStatis.size() > 0){
            for (DayStatis dayStatis : userDayStatis) {
                set.add(dayStatis.getDayStr());
            }
        }
        //把set集合转换为list集合
        ArrayList<String> list = new ArrayList<>(set);
        Collections.sort(list);
        return list.toArray(new String[]{});
    }

    private BigDecimal[] fetchOrderAmt(List<DayStatis> orderDayStatis, String[] dayData) {
        BigDecimal[] arr = new BigDecimal[dayData.length];
        if(orderDayStatis != null && orderDayStatis.size() > 0){
            for (int i = 0; i < dayData.length; i++) {
                for (DayStatis dayStatis : orderDayStatis) {
                    String dayStr = dayStatis.getDayStr();
                    if(dayData[i].equalsIgnoreCase(dayStr)){
                        arr[i] = dayStatis.getAmts();
                        break;
                    }
                }
            }
        }
        return arr;
    }

    private int[] fetchCnt(List<DayStatis> orderDayStatis, String[] dayData) {
        int[] arr = new int[dayData.length];
        if(orderDayStatis != null && orderDayStatis.size() > 0){
            for (int i = 0; i < dayData.length; i++) {
                for (DayStatis dayStatis : orderDayStatis) {
                    String dayStr = dayStatis.getDayStr();
                    if(dayData[i].equalsIgnoreCase(dayStr)){
                        arr[i] = dayStatis.getCnts();
                        break;
                    }
                }
            }
        }
        return arr;
    }

    private String[] fetchDayData(List<DayStatis> orderDayStatis) {
        List<String> dayList = new ArrayList<>();
        //提取数据中的日期字符串 ，存储到集合中
        if(orderDayStatis != null && orderDayStatis.size() > 0){
            for (DayStatis dayStatis : orderDayStatis) {
                String dayStr = dayStatis.getDayStr();
                dayList.add(dayStr);
            }
        }
        //升序排序
        Collections.sort(dayList);
        //把集合转换为数组
        String[] dayArr = dayList.toArray(new String[]{});

        return dayArr;
    }

    private List<String> fetchCategoryNames(List<CategorySellAmts> categorySellAmtsList) {
        //创建返回值对象
        List<String>  names = new ArrayList<>();


        //遍历集合 ，取出所有的names，存储到集合中，并返回
        if(categorySellAmtsList != null && categorySellAmtsList.size() > 0){
            for (CategorySellAmts categorySellAmts : categorySellAmtsList) {

                names.add( categorySellAmts.getName());
            }
        }

        return names;
    }

    @GetMapping
    public Object info(){

        int goodsTotal = goodsService.queryTotal();
        int userTotal = userService.queryTotal();
        int productTotal = productService.queryTotal();
        int orderTotal = orderService.queryTotal();



        Map<String,Object> data = new HashMap<>();
        data.put("goodsTotal", goodsTotal);
        data.put("userTotal", userTotal);
        data.put("productTotal", productTotal);
        data.put("orderTotal", orderTotal);

        return ResponseUtil.ok(data);
    }
}
